/**
 * 
 */
package gpl.scotlandyard.android.activities;

import gpl.scotlandyard.android.R;
import gpl.scotlandyard.android.controllers.SimpleTrackerController;
import gpl.scotlandyard.beans.MarkedNode;
import gpl.scotlandyard.beans.basics.Ticket;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * @author norbert
 */
public class SimpleTrackerActivity extends AbstractActivity {

  private static final String TAG = SimpleTrackerActivity.class.getName();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.simple_tracker);
    setController(SimpleTrackerController.getInstance());
    getController().setActivity(this);

    EditText editPosition = (EditText) findViewById(R.id.simpleTracker_text_position);
    editPosition.setOnEditorActionListener(new OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        return getController().onPlayerAppears(v.getText().toString(), actionId, event);
      }
    });

    Button button = (Button) findViewById(R.id.simpleTracker_but_taxi);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getController().onClickTicketButton(v, Ticket.TAXI);
      }
    });

    button = (Button) findViewById(R.id.simpleTracker_but_bus);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getController().onClickTicketButton(v, Ticket.BUS);
      }
    });

    button = (Button) findViewById(R.id.simpleTracker_but_ug);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getController().onClickTicketButton(v, Ticket.UG);
      }
    });

    button = (Button) findViewById(R.id.simpleTracker_but_hide);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getController().onClickTicketButton(v, Ticket.HIDE);
      }
    });

    button = (Button) findViewById(R.id.simpleTracker_but_rollback);
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        getController().onClickRollback();
      }
    });

    GridView gridview = (GridView) findViewById(R.id.simpleTracker_grid);
    registerForContextMenu(gridview);
    getController().onViewCreated();
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getController().onCreateContextMenu(menu, v, menuInfo);
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.cm_node_evaluation_hopefully:
        getController().onNodeMarked(item, MarkedNode.HOPEFULLY_MARK);
        break;
      case R.id.cm_node_evaluation_hopeless:
        getController().onNodeMarked(item, MarkedNode.HOPELESS_MARK);
        break;
      case R.id.cm_node_evaluation_neutral:
        getController().onNodeMarked(item, MarkedNode.NEUTRAL_MARK);
        break;
      case R.id.cm_node_evaluation_remove:
        getController().onNodeMarked(item, MarkedNode.IMPOSSIBLE_MARK);
        break;
      default:
        return super.onContextItemSelected(item);
    }
    return true;
  }

  /**
   * @param taxi
   * @param bus
   * @param ug
   * @param hide
   * @param possibilities
   * @param turn
   * @param canRollback
   */
  public void refreshView(boolean taxi, boolean bus, boolean ug, boolean hide, int possibilities, int turn, boolean canRollback) {
    Button button = (Button) findViewById(R.id.simpleTracker_but_taxi);
    button.setEnabled(taxi);

    button = (Button) findViewById(R.id.simpleTracker_but_bus);
    button.setEnabled(bus);

    button = (Button) findViewById(R.id.simpleTracker_but_ug);
    button.setEnabled(ug);

    button = (Button) findViewById(R.id.simpleTracker_but_hide);
    button.setEnabled(hide);

    TextView text = (TextView) findViewById(R.id.simpleTracker_possibilities);
    text.setText(possibilities + "");

    EditText editPosition = (EditText) findViewById(R.id.simpleTracker_text_position);
    editPosition.setText("");

    text = (TextView) findViewById(R.id.simpleTracker_turn);
    button = (Button) findViewById(R.id.simpleTracker_but_rollback);
    button.setEnabled(canRollback);
    if (turn > 0) {
      text.setText(turn + "");
    } else {
      text.setText(R.string.kw_hide_short);
    }
    text.invalidate();
  }

  @Override
  public SimpleTrackerController getController() {
    return (SimpleTrackerController) super.getController();
  }

  @Override
  protected void onCreateOptionsMenuChild(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.quick_tracker_menu, menu);

  }

  @Override
  protected boolean onOptionsItemSelectedChild(MenuItem item) {
    if (item.getItemId() == R.id.option_tracker_sort) {
      if (item.isChecked()) {
        item.setChecked(false);
        getController().sortListByNumber();
      } else {
        item.setChecked(true);
        getController().sortListByMark();
      }
      return true;
    } else if (item.getItemId() == R.id.option_tracker_reset_mark) {
      getController().clearMarks();
      return true;
    }
    // do nothing
    return false;
  }

}
